//
//  EleSpeechDetailViewController.m
//  Conference
//
//  Created by chenxin on 12-11-19.
//
//

#import "EleSpeechDetailViewController.h"
#import "Globals.h"
#import "AsyncImageView.h"
#import "NetContant.h"
#import "ShowImageController.h"
@interface EleSpeechDetailViewController ()

@end

@implementation EleSpeechDetailViewController
@synthesize eleSpeakData;
@synthesize img_head;
@synthesize lab_title;
@synthesize lab_speak;
@synthesize lab_time;
@synthesize lab_content;
@synthesize scrollview;
@synthesize Height;
@synthesize arrayOfUrl;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
        [super viewDidLoad];
    if(nil!=self.eleSpeakData){
    
        NSString *headUrl = eleSpeakData.headUrl;
        if (nil !=headUrl && ![@"" isEqualToString:headUrl] ){}NSString *content=[NSString stringWithFormat:@"%@",self.eleSpeakData.message];
        
        
        UIFont *font = [UIFont systemFontOfSize:16];
        
        CGSize size=[[Globals getInstance]CalculationCellSize:self.scrollview.frame.size.width fontSize:16 content:content];
        
        CGRect rect = [self.lab_content textRectForBounds:self.lab_content.frame limitedToNumberOfLines:0];
        rect.size=size;
        self.lab_content.frame = rect;
        self.lab_content.text=content;
        self.lab_content.numberOfLines = 0;
        self.lab_content.font = font;
        
        self.lab_title.text=self.eleSpeakData.title;
        self.lab_speak.text=self.eleSpeakData.speak;
        self.lab_time.text=self.eleSpeakData.dateTime;
        
        self.Height=size.height+20;
        
        [self.scrollview setContentSize:CGSizeMake(self.scrollview.frame.size.width, self.Height)];
        
        //
        NSString *imgUri=self.eleSpeakData.arrImg;
        
        if(nil!=imgUri||![imgUri isEqualToString:@""]){
            NSArray *img=[imgUri componentsSeparatedByString:[NetContant getInstance].IMGURL_SPLIT_FLAG];
            
            self.arrayOfUrl=[[Globals getInstance]addDefaultImage:img scroll:self.scrollview height:self.Height];
            
            NSThread *thread=[[[NSThread alloc] initWithTarget:self selector:@selector(run:) object:img] autorelease];
            [thread start];
        }

        
}
    // Do any additional setup after loading the view from its nib.
}

-(void)run:(NSArray*)img{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSMutableArray *arrayOfImage=[[Globals getInstance]getImageArray:img];
    [self performSelectorOnMainThread:@selector(onPostExecute:) withObject:arrayOfImage waitUntilDone:YES];
    
	[pool release];
    
}

-(void)onPostExecute:(NSMutableArray *)arrayOfImage{
    NSArray *views = [self.scrollview subviews];
    for(id view in views)
    {
        if([view isKindOfClass:[UIImageView class]])
            [view removeFromSuperview];
    }

    for(int i=0;i<[arrayOfImage count];i++){
        UIImageView *imageView=[arrayOfImage objectAtIndex:i];
        imageView.frame=CGRectMake(0, self.Height, imageView.frame.size.width, imageView.frame.size.height);
        imageView.center=CGPointMake(140, imageView.center.y);
        
//         NSLog(@"width=%f,height=%f",imageView.frame.size.width,imageView.frame.size.height);
        
        [self.scrollview addSubview:imageView];
        self.Height=self.Height+imageView.frame.size.height+20;
        //点击
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(UesrClicked:)];
        [imageView addGestureRecognizer:singleTap];
        [singleTap release];
        
        [self.scrollview setContentSize:CGSizeMake(self.scrollview.frame.size.width, self.Height)];
    }
}

-(void)UesrClicked:(UIGestureRecognizer *)gestureRecognizer{
    UIImageView *imageView = (UIImageView *)[gestureRecognizer view];
    int index = imageView.tag;
  
    ShowImageController *imgController=[[ShowImageController alloc]initWithNibName:@"ShowImageController" bundle:nil];
    imgController.url=[self.arrayOfUrl objectAtIndex:index];
//    [self presentModalViewController:imgController animated:YES];
    
    [self.navigationController pushViewController:imgController animated:YES];
    [imgController release];
}

- (IBAction)btnBackAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [arrayOfUrl release];
    [img_head release];
    [lab_title release];
    [lab_speak release];
    [lab_time release];
    [lab_content release];
    [scrollview release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setArrayOfUrl:nil];
    [self setImg_head:nil];
    [self setLab_title:nil];
    [self setLab_speak:nil];
    [self setLab_time:nil];
    [self setLab_content:nil];
    [self setScrollview:nil];
    [super viewDidUnload];
}
@end
